<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>⼩⽶轮播图</title>
    <style>
        body {
            margin: 0;
        }
        #banner {
            width: 1226px;
            height: 460px;
            /* background: aqua; */
            margin: 20px auto;
            /*⾯板使⽤相对定位，⽅便在其上⾯使⽤绝对定位添加元素*/
            position: relative;
        }
        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }
        #banner ul li img {
            width: 1226px;
            height: 460px;
        }
        #banner .item {
            position: absolute;
            top: 0;
            display: none;
            opacity: 0;
            animation-timing-function: ease-in-out;
        }
        /*初始加载时，显⽰第⼀张图⽚*/
        #banner .item:first-child {
            display:block;
            opacity: 1;
        }
        .arrow #leftArrow, .arrow #rightArrow {
            width: 40px;
            height: 69px;
            position: absolute;
            top: 195px;
        }
        .arrow #leftArrow {
            left: 100px;
            background-image: url("../images/xiaomi/icon-slides.png");
            background-position: -83px 0;
            background-repeat: no-repeat;
        }
        .arrow #rightArrow {
            right: 100px;
            background-image: url("../images/xiaomi/icon-slides.png");
            background-position: -126px 0;
            background-repeat: no-repeat;
        }
        /*右下⾓⼩圆点切换*/
        .zeroPoint {
            position: absolute;
            bottom: 20px;
            right: 40px;
        }
        .zeroPoint li {
            width: 5px;
            height: 5px;
            border: 2px solid #555;
            background: #555;
            border-radius: 50%;
            display: inline-block;
            margin-right: 5px;
        }
  
       
        .zeroPoint li:hover {
            background: #fff;
        }
        .zeroPoint li.hover {
            background: #fff;
        }
        /*定义图⽚切换动画样式*/
        /*淡出*/
        @keyframes fadeOut {
            from {
                opacity: 1;
            }
            to {
                opacity: 0;
            }
        }
        @-webkit-keyframes fadeOut {
            from {
                opacity: 1;
            }
            to {
                opacity: 0;
            }
        }
        /*淡⼊*/
        @keyframes fadeIn{
            from{
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }
        @-webkit-keyframes fadeIn{
            from{
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }
    </style>
</head>
<body>
<!--整个容器-->
<div id="banner">

<!--图⽚容器-->
<div class="img-wrap">
    <ul>
        <li class="item"><img src="./images/b_01.jpg" alt=""></li>
        <li class="item"><img src="./images/b_02.jpg" alt=""></li>
        <li class="item"><img src="./images/b_03.jpg" alt=""></li>
        <li class="item"><img src="./images/b_04.jpg" alt=""></li>
        <li class="item"><img src="./images/b_05.jpg" alt=""></li>
    </ul>
</div>
<!--左右箭头容器-->
<div class="arrow">
    <div id="leftArrow">&lt;</div>
    <div id="rightArrow">&gt;</div>
</div>
<!--右下⾓⼩圆点-->
<ul class="zeroPoint">
    <li class="hover"></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>




</div>
</body>
</html>



<script>

//<!--这个index是图⽚和⼩圆点进⾏同步的关键-->

var index = 0;
    //获取⼩圆点的⽗元素ul
    var zeroPoint = document.querySelector(".zeroPoint")



//获取所有图⽚
var imgLis = document.querySelectorAll(".img-wrap li");
    //获取所有的⼩圆点
    let lis = zeroPoint.querySelectorAll("li");
    //⼩圆点点击事件监听
    zeroPoint.addEventListener("click", function (event) {
        let target = event.target;
        for (let [n, li] of lis.entries()) {
            if (li === target) {//更改被点击的⼩圆点的样式，背景⾊为⽩⾊，这⾥通过添加hover类的⽅式实现
                index = n;
                target.className = "hover";
            } else {//否则恢复为⼩圆点的默认样式，这⾥通过去除hover类的⽅式来实现
                li.className = li.className.replace("hover", "");
            }
        }
        setPicture();//设置图⽚
    });
    //左箭头点击事件
    var leftArrow = document.querySelector("#leftArrow");
    leftArrow.addEventListener("click", function (event) {
        index--;//点击左箭头时，index--
        if (index < 0) {//若index < 0 则赋值为4，达到循环的效果
            index = 4;
        }
        setZero();//设置⼩圆点
        setPicture();//设置图⽚
    });
    //右箭头点击事件
    var rightArrow = document.querySelector("#rightArrow");
    rightArrow.addEventListener("click", function (event) {
        index++;
        if (index > 4) {
            index = 0;
        }
        setZero();
        setPicture();
    });
    function setPicture() {
        //遍历所有图⽚
        for (let i = 0, length = imgLis.length; i < length; i++) {
            if (i === index) {//如果这个图⽚是第index个孩⼦，则让它显⽰，并设置淡⼊的动画效果
                imgLis[i].style.display = "block";
                imgLis[i].style.animation = "fadeIn 3s";
            } else {//否则，隐藏，并设置淡出动画
                imgLis[i].style.animation = "fadeout 0s";
                imgLis[i].style.display = "none"
            }
        }
    }
    function setZero() {
        for (let [n, li] of lis.entries()) {
            if (n === index) {//如果是第index个⼩圆点，则修改这个⼩圆点的背景颜⾊为⽩⾊
                li.className = "hover";
            } else {//其他⼩圆点恢复默认样式
                li.className = li.className.replace("hover", "");
            }
        }
    }
    setInterval(function () {//定时器，达到⾃动轮播的效果
        index++;//循环++
        if (index > 4) {//当循环到最后⼀个就再从第⼀个开始
            index = 0;
        }
        setZero();//设置⼩圆点
        setPicture();//设置图⽚
    }, 3000);
</script>
